package com.example.makerspace.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.makerspace.domain.LanguageDO;
import com.example.makerspace.domain.ProjectTypeDO;
import com.example.makerspace.dto.Result;
import com.example.makerspace.dto.project.ProjectTypeBaseDTO;
import com.example.makerspace.mapper.ProjectTypeMapper;
import com.example.makerspace.service.LanguageService;
import com.example.makerspace.service.ProjectTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.example.makerspace.enums.ResultCodeEnum.LANGUAGE_EXIST;

@Service
public class ProjectTypeServiceImpl implements ProjectTypeService {

    @Autowired
    private ProjectTypeMapper projectTypeMapper;

    @Override
    public Result create(ProjectTypeBaseDTO projectTypeBaseDTO) {
        QueryWrapper<ProjectTypeDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type_name", projectTypeBaseDTO.getTypeName());
        List<ProjectTypeDO> languageDOS = projectTypeMapper.selectList(queryWrapper);
        if (languageDOS.size() > 0) {
            return Result.success(LANGUAGE_EXIST);
        }
        ProjectTypeDO projectTypeDO = new ProjectTypeDO();
        projectTypeDO.setTypeName(projectTypeBaseDTO.getTypeName());
        projectTypeMapper.insert(projectTypeDO);
        return Result.success(true);
    }

    @Override
    public Result query() {
        QueryWrapper<ProjectTypeDO> queryWrapper = new QueryWrapper<>();
        List<ProjectTypeDO> projectTypeDOS = projectTypeMapper.selectList(queryWrapper);
        return Result.success(projectTypeDOS);
    }

    @Override
    public Result delete(Integer id) {
        projectTypeMapper.deleteById(id);
        return Result.success(true);
    }

    @Override
    public Result update(ProjectTypeBaseDTO projectTypeBaseDTO) {
        QueryWrapper<ProjectTypeDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type_name", projectTypeBaseDTO.getTypeName());
        List<ProjectTypeDO> languageDOS = projectTypeMapper.selectList(queryWrapper);
        if (languageDOS.size() > 0) {
            return Result.success(LANGUAGE_EXIST);
        }
        ProjectTypeDO projectTypeDO = new ProjectTypeDO();
        projectTypeDO.setTypeName(projectTypeBaseDTO.getTypeName());
        projectTypeDO.setId(projectTypeBaseDTO.getId());
        projectTypeMapper.updateById(projectTypeDO);
        return Result.success(true);
    }
}
